public class IfStmt extends Statement
{
	DataInfo dataInfo;
	
	Expression expr;
	Statement thenStmt;
	Statement elseStmt;

	public IfStmt( Expression expr, Statement thenStmt, Statement elseStmt, DataInfo dataInfo )
	{
		this.expr = expr;
		this.thenStmt = thenStmt;
		this.elseStmt = elseStmt;
		
		this.dataInfo = dataInfo;
	}
	
	public String toString()
	{
		String result = "IfStmt \n";
		String tbr = "";
		tbr += "| " + expr;
		tbr += "| " + thenStmt.toString();
		if(elseStmt!= null)
		{
			tbr+="else \n";
			tbr += "| "+elseStmt.toString();
		}
		
		
		for( String s: tbr.split("\n"))
		{
			result += "| " +s + "\n";
		}
		return result;
	}
	
	public void check() throws SemanticException {
		
		if(!expr.getType().equalsIgnoreCase("boolean"))
		{
			String details = "The condition of the if statement is not of type boolean.";
			if( dataInfo != null )
				throw new SemanticException( dataInfo.lineNumber, dataInfo.charNumber, dataInfo.line, details );
			else
				throw new SemanticException( details );
		}
	
		
		thenStmt.methodName = this.methodName;
		
		if(elseStmt != null)
			elseStmt.methodName = this.methodName;
		
		SymbolTable.getInstance().openScope();
		
			expr.check();
		
			thenStmt.check();
			
			if(elseStmt != null)
				elseStmt.check();
		
		SymbolTable.getInstance().closeScope();
		
	}
	
	public boolean willReturn(){
		
		if(elseStmt != null)
			return(thenStmt.willReturn() && elseStmt.willReturn());
		
		return(false);
		
	}
	
}
